from spacy.language import Language
from spacy_langdetect import LanguageDetector
import spacy


class LanguageDetection:
    def __init__(self):
        self._nlp = self.load_nlp()

    @Language.factory("language_detector")
    def get_lang_detector(nlp, name):
        return LanguageDetector()

    def detect_lang(self, text):
        self._language = self._nlp(text)._.language["language"]
        return self._language

    @staticmethod
    def load_nlp() -> Language:
        nlp = spacy.load("en_core_web_lg")
        nlp.add_pipe("language_detector", last=True)
        return nlp


if __name__ == "__main__":
    lang_detect = LanguageDetection()
    detected_lang = lang_detect.detect_lang("许换卷")
    print(detected_lang)
